<?php

  class SqlServer extends Base implements ISqlServer {

    private $handle;
    private $hostname;
    private $login;
    private $password;
    private $database;

    public function gethostname() {
      return $this->hostname;
    }

    public function getLogin() {
      return $this->login;
    }

    public function getPassword() {
      return $this->password;
    }

    public function getDatabase() {
      return $this->database;
    }

    function __construct($hostname, $login, $password, $database) {
      $this->hostname = $hostname;
      $this->login = $login;
      $this->password = $password;
      $this->database = $database;

      $this->connect();
    }

    public function connect($options = null) {
      if (is_null($options)) {
        $options = array("Database" => $this->database,
            "PWD" => $this->password,
            "UID" => $this->login);
      }
      $this->handle = sqlsrv_connect($this->hostname, $options);
    }

    public function executeQuery($query, $params = null, $options = null) {
      if (is_null($params)) {
        return sqlsrv_query($this->handle, $query);
      } else if (is_null($options)) {
        return sqlsrv_query($this->handle, $query, $params);
      } else {
        return sqlsrv_query($this->handle, $query, $params, $options);
      }
    }

    public function fetchArray($resource, $type = SqlServer::FetchBoth, $row = null, $offset = null) {
      if (is_null($row)) {
        return sqlsrv_fetch_array($resource, $type);
      } else if (is_null($offset)) {
        return sqlsrv_fetch_array($resource, $type, $row);
      } else {
        return sqlsrv_fetch_array($resource, $type, $row, $offset);
      }
    }

    public function getAffecteRows() {
      return sqlsrv_rows_affected($this->handle);
    }

    public function getErrorMessage() {
      return sqlsrv_errors();
    }

    public function isConnected() {
      return is_null($this->handle);
    }

  }

?>
